namespace FIREFLYX {
    /** 后渲染 一张纹理 着色器 */
    export const BUILTIN_SHADER_SKY_BOX = `skyBox`;
    DefaultAsset.shaderGenMap[BUILTIN_SHADER_SKY_BOX] = () => {
        const vsCode = `#version 300 es
            precision highp float;
            precision highp int;
            layout (location = 0) in vec4 a_Position; //顶点 位置 是有默认值的 (0,0,0,1)
            
            out vec4 v_position;
            void main() {
                // vec4 pos = vec4(a_Position.xyz , 1.0);
                v_position = a_Position;
                gl_Position = a_Position;
                gl_Position.z = 0.9999;    //深度设置为最大
            }
        `;

        const fsCode = `#version 300 es
            precision highp float;
            precision highp int;

            uniform samplerCube u_skybox;

            uniform ubo_cam_block{
                mat4 u_viewMtx;                     //视窗矩阵
                mat4 u_projectMtx;                  //投影矩阵
                vec4 u_eyePos;                      //眼睛位置
            } uboCam;

            #define ViewMatrix uboCam.u_viewMtx
            #define ProjectMatrix uboCam.u_projectMtx
            #define SkyBoxTex u_skybox

            in vec4 v_position;
            out vec4 color;
            void main(){
                mat4 viewDirction = mat4(ViewMatrix); 
                // 我们只关心方向所以清除移动的部分
                viewDirction[3][0] = 0.0;
                viewDirction[3][1] = 0.0;   
                viewDirction[3][2] = 0.0;

                mat4 viewDirectionProjectionInverse = inverse(ProjectMatrix * viewDirction);
                vec4 t = viewDirectionProjectionInverse * v_position;
                vec4 texColor = texture(SkyBoxTex , (t.xyz / t.w));    //除以w转换4D到3D
                color = texColor;
            }
        `;
        //glsl
        const vs = new GLSL();
        vs.setCode(vsCode);
        const fs = new GLSL();
        fs.setCode(fsCode);

        //shader
        let result: Shader = new Shader();
        result.tag = BUILTIN_SHADER_SKY_BOX;
        result.lightMode = RenderPipeline.LM_FORWARD;
        result.vertexGLSL = vs;
        result.fragmentGLSL = fs;
        //unifrom 默认值
        result.definePropertyMap = {};

        // result.definePropertyMap.u_mainTex = {
        //     type: "texture",
        //     value: "White"
        // };

        //apply
        result.apply();

        return result;
    };
}